package org.w515.lab0_backend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.stereotype.Service;
import org.w515.lab0_backend.dto.ProductCreateDTO;
import org.w515.lab0_backend.dto.ProductPageQueryDTO;
import org.w515.lab0_backend.converter.ProductConverter;
import org.w515.lab0_backend.entity.Product;
import org.w515.lab0_backend.mapper.ProductMapper;
import org.w515.lab0_backend.service.ProductService;
import org.w515.lab0_backend.vo.ProductItemVO;
import org.w515.lab0_backend.vo.ProductPageVO;

@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    @Override
    public ProductPageVO listProducts(ProductPageQueryDTO queryDTO) {
        int pageNum = queryDTO.getPage() > 0 ? queryDTO.getPage() : 1;
        int pageSize = queryDTO.getSize() > 0 ? queryDTO.getSize() : 10;

        Page<Product> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();

        String name = queryDTO.getName();
        if (name != null && !name.isBlank()) {
            wrapper.like(Product::getName, name);
        }

        boolean asc = "asc".equalsIgnoreCase(queryDTO.getSortOrder());
        String sortBy = queryDTO.getSortBy();
        if (sortBy != null && !sortBy.isBlank()) {
            switch (sortBy) {
                case "name" -> wrapper.orderBy(true, asc, Product::getName);
                case "version" -> wrapper.orderBy(true, asc, Product::getVersion);
                case "vendor" -> wrapper.orderBy(true, asc, Product::getVendor);
                case "packageManager" -> wrapper.orderBy(true, asc, Product::getPackageManager);
                case "createTime" -> wrapper.orderBy(true, asc, Product::getCreateTime);
                case "updateTime" -> wrapper.orderBy(true, asc, Product::getUpdateTime);
                default -> {
                    wrapper.orderByDesc(Product::getId);
                }
            }
        } else {
            // 默认按Id倒序
            wrapper.orderByDesc(Product::getId);
        }

        Page<Product> result = this.page(page, wrapper);
        List<ProductItemVO> records = result.getRecords().stream()
            .map(ProductConverter::toItemVO)
            .collect(Collectors.toList());

        ProductPageVO vo = new ProductPageVO();
        vo.setTotal(result.getTotal());
        vo.setPage((int) result.getCurrent());
        vo.setSize((int) result.getSize());
        vo.setRecords(records);
        return vo;
    }

    @Override
    public Long createProduct(ProductCreateDTO dto) {
        Product product = ProductConverter.fromCreateDTO(dto);
        this.save(product);
        return product.getId();
    }

    @Override
    public boolean deleteProduct(Long id) {
        return this.removeById(id);
    }

    @Override
    public List<ProductItemVO> searchProducts(String name) {
        if (name == null || name.isBlank()) {
            return Collections.emptyList();
        }
        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Product::getName, name);
        List<Product> list = this.list(wrapper);
        return ProductConverter.toItemVOList(list);
    }
}
